/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aeronef.builder;

import aeronef.appStates.GameState;
import aeronef.effects.SmokeEmitter;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import org.w3c.dom.Document;

/**
 *
 * @author Hagan
 */
public class FunnelBuilder extends AbstractBuilder {
 
    public FunnelBuilder(GameState gameState) {
        super(gameState);
    }
        
    public Node buildFromDefinition(String name, String type) {

        String definitionString = (String) assetManager.loadAsset("Data/ships/funnels/" + type + ".xml");
        definition = buildDocument(definitionString);
        Node funnel = createFunnel();

        return funnel;
    }
    
    private Node createFunnel() {

        String mesh = getTagValue("mesh", definition.getDocumentElement());

        Node funnelNode = new Node();
        Spatial funnel = assetManager.loadModel("Models/ships/funnels/" + mesh + ".j3o");
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
        funnel.setMaterial(mat);
        funnelNode.attachChild(funnel);
        attachSmoke(definition, funnelNode);
        return funnelNode;
    }
    
    private void attachSmoke(Document doc, Node funnelNode) {
        
        Node smokeNode = new Node();
        SmokeEmitter smoke = new SmokeEmitter("Emitter", 30);
        smoke.setNumParticles(100);
        smoke.getParticleInfluencer().setVelocityVariation(0.1f);
        smoke.getParticleInfluencer().setInitialVelocity(new Vector3f(0, 1, 0));
        
        Material part_mat = new Material(assetManager, "Common/MatDefs/Misc/Particle.j3md");
        part_mat.setTexture("Texture", assetManager.loadTexture("Effects/smoke_puff.png"));
        smoke.setMaterial(part_mat);
        smokeNode.attachChild(smoke);

        Float smokeXPos = Float.parseFloat(getTagValue("smokeXPos", doc.getDocumentElement()));
        Float smokeYPos = Float.parseFloat(getTagValue("smokeYPos", doc.getDocumentElement()));
        Float smokeZPos = Float.parseFloat(getTagValue("smokeZPos", doc.getDocumentElement()));
        
        funnelNode.attachChild(smokeNode);
        smokeNode.move(smokeXPos, smokeYPos, smokeZPos);
    }
}
